package pl.vhcreator;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

import javax.swing.JComponent;
import javax.swing.JProgressBar;
import pl.vhcreator.domain.Config;

import pl.vhcreator.domain.HostWriter;
import pl.vhcreator.domain.VirtualWriter;

public class Application implements Runnable
{
    private JFrame frame;
    private JButton saveCreatorButton;
    private JButton saveOptionsButton;
    private JTextField hostNameField;
    private JTextField hostPath; 
    private JTextField virtualPath;
    private JTextField projectsPath; 
    
    private JLabel urlLabel;
    private JLabel virtualPathLabel;
    private JLabel hostsPathLabel;
    private JLabel projectPathLabel;
    private JLabel infoLabel;
    private JProgressBar progress;
    
    private String virtualPathValue;
    private String hostPathValue;
    private String projectsPathValue;

    private Config config;
    
    public void run() 
    {
        // frame init 
        frame = new JFrame();
        frame.setSize(450, 250);
        frame.setTitle("JVHost - v.0.1 Beta - A. Świerc");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        // info
        infoLabel = new JLabel();
        progress = new JProgressBar();
        
        // config init 
        initConfiguration();
          
        // field init
        saveCreatorButton = new JButton("Save");
        saveOptionsButton = new JButton("Save");
        hostNameField = new JTextField(20);
        hostPath = new JTextField(20);
        virtualPath = new JTextField(20);
        projectsPath = new JTextField(20);
        
        // field values 
        hostPath.setText(hostPathValue);
        virtualPath.setText(virtualPathValue);
        projectsPath.setText(projectsPathValue);
        
        // label init 
        urlLabel = new JLabel("URL: ");
        virtualPathLabel = new JLabel("Virtual path: ");
        hostsPathLabel = new JLabel("Host path: ");
        projectPathLabel = new JLabel("Project path: ");
        
        // labels for fields 
        urlLabel.setLabelFor(hostNameField);
        virtualPathLabel.setLabelFor(virtualPath);
        hostsPathLabel.setLabelFor(hostPath);
        projectPathLabel.setLabelFor(projectsPath);
        
        // init action
        saveCreatorButton.addActionListener(new SaveCreatorAction());
        saveOptionsButton.addActionListener(new SaveOptionsAction());
        
        // create tab panels 
        JTabbedPane tabPanel = new JTabbedPane();
        
        // creator tab
        JPanel cLabelPanel = new JPanel(new GridLayout(1, 1));
        cLabelPanel.add(urlLabel);
      
        JPanel cFieldPanel = new JPanel(new GridLayout(1, 1));
        cFieldPanel.add(hostNameField);
        
        JPanel labelFieldPanel = new JPanel();
        labelFieldPanel.setSize(2, 1);
        labelFieldPanel.add(cLabelPanel, BorderLayout.CENTER);
        labelFieldPanel.add(cFieldPanel, BorderLayout.LINE_END);
        
        JPanel buttonCreatorPanel = new JPanel();
        buttonCreatorPanel.add(saveCreatorButton);
        
        JPanel infoPanel = new JPanel();
        infoPanel.add(infoLabel);
        
        JPanel creatorPanel = new JPanel(new GridLayout(2,1));
        creatorPanel.add(labelFieldPanel);
        creatorPanel.add(buttonCreatorPanel);
         
        tabPanel.addTab("Creator", creatorPanel);
        
        // configuration tab
        JPanel oLabelPanel = new JPanel(new GridLayout(3, 1));
        oLabelPanel.add(virtualPathLabel);
        oLabelPanel.add(hostsPathLabel);
        oLabelPanel.add(projectPathLabel);
        
        JPanel oFieldPanel = new JPanel(new GridLayout(3, 1));
        oFieldPanel.add(virtualPath);
        oFieldPanel.add(hostPath);
        oFieldPanel.add(projectsPath);
        
        JPanel oLabelFieldPanel = new JPanel();
        oLabelFieldPanel.setSize(3, 1);
        oLabelFieldPanel.add(oLabelPanel, BorderLayout.CENTER);
        oLabelFieldPanel.add(oFieldPanel, BorderLayout.LINE_END);
        
        JPanel buttonOptionsPanel = new JPanel();
        buttonOptionsPanel.add(saveOptionsButton);
        
        JPanel optionPanel = new JPanel(new GridLayout(2,1));
        optionPanel.add(oLabelFieldPanel);
        optionPanel.add(buttonOptionsPanel);
        
        tabPanel.addTab("Configuration", optionPanel);
        
        // this little application is too fast for progress bar ;)
        //frame.getContentPane().add(progress, BorderLayout.SOUTH);
        tabPanel.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
        frame.getContentPane().add(tabPanel);  
        frame.getContentPane().add(infoPanel, BorderLayout.SOUTH);
        frame.setVisible(true);
    }
    
    private void initConfiguration()
    {
        try { 
            config = new Config();
            virtualPathValue = config.getValue("virtual_path");
            hostPathValue = config.getValue("host_path");
            projectsPathValue = config.getValue("projects_path");
                   
            if (null == virtualPathValue || null == hostPathValue) {
                infoLabel.setText("<html><div width=400>First configure your apache paths.</div>");   
            }
                    
        } catch (IOException e) {
            infoLabel.setText("<html><div width=400>" + e.getMessage() + "</div>");     
        }
    }
    
    private class SaveCreatorAction implements ActionListener
    {
        public void actionPerformed(ActionEvent a) 
        { 
            HostWriter hostWriter = new HostWriter();
            hostWriter.setPathToHost(hostPath.getText().trim());       
            VirtualWriter virtualWriter = new VirtualWriter();
            
            infoLabel.setText("");
     
            String url = hostNameField.getText().trim();
            if (! url.isEmpty()) {
                hostWriter.setHostName(url);
            }

            boolean hasWriteHost = hostWriter.writeNewAdress();
            if (hasWriteHost) {        
                // zapisanie virtualki
                virtualWriter.setAddress(hostWriter.getAddress());
                virtualWriter.setHostName(hostWriter.getHostName());
                virtualWriter.setPathToProjects(projectsPath.getText().trim());
                virtualWriter.setPathToVirtual(virtualPath.getText().trim());
                virtualWriter.writeNewVirtual();
            } 
            
            String msgOutput = "";
            for (String hwLine : hostWriter.getMessage()) {
                if (! hwLine.isEmpty()) { 
                    msgOutput += "- " + hwLine + "<br />";
                }
            }
            
            for (String vwLine : virtualWriter.getMessage()) {
                if (! vwLine.isEmpty()) {
                    msgOutput += "- " + vwLine + "<br />";
                }
            }
            
            infoLabel.setText("<html><div width=400>" + msgOutput + "</div>");        
        }
    }
    
    private class SaveOptionsAction implements ActionListener 
    {
        public void actionPerformed(ActionEvent even) 
        {
            try { 
                hostPathValue = hostPath.getText().trim();
                config.setValue("host_path", hostPathValue);
                
                virtualPathValue = virtualPath.getText().trim();
                config.setValue("virtual_path", virtualPathValue);
                
                projectsPathValue = projectsPath.getText().trim();
                config.setValue("projects_path", projectsPathValue);
                
                infoLabel.setText("<html><div width=400>configuration has been successfully saved</div>"); 
            } catch (Exception e) {
                infoLabel.setText("<html><div width=400>" + e.getMessage() + "</div>");   
            }
        }
    }
}
